﻿using Microsoft.Extensions.DependencyInjection;
using Quartz;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.WCS.HangfireHost.TaskJobs;

namespace Zocono.WCS.HangfireHost.QuartzJobs
{
    public class QuartzInitPlcConnectPoolJob : IJob
    {
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public QuartzInitPlcConnectPoolJob(IServiceScopeFactory serviceScopeFactory)
        {
            this._serviceScopeFactory = serviceScopeFactory;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            var dataMap = context.JobDetail.JobDataMap;
            string myPrograme = dataMap.GetString("myPrograme");
            //将所有程序放到一个集合中，方便plc连接
            List<string> list = myPrograme.Split("-").ToList();
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var _InitPlcConnectPoolJob = scope.ServiceProvider.GetRequiredService<InitPlcConnectPoolJob>();
                await _InitPlcConnectPoolJob.DoJob(list);
            }
        }
    }
}
